package com.agann.util;

import java.util.LinkedList;

import com.agann.model.Memory;
import com.agann.model.Record;

public class WmR extends Memory{

	/**
	 * Type Code
	 */
	public static final String TYPE_CODE = "WmR";
	public static final String WmR_SCORE = "wm%r_score";
	
	private int n;
	private LinkedList<Record> wHistory;

	public WmR(String code) {
		super(code);
		String[] args = code.split("@");
		n = Integer.valueOf(args[1]);
		wHistory = new LinkedList<Record>();
	}

	/**
	 * Set Score to NaN when the number of records
	 * are less than n.
	 * 
	 */
	@Override
	public void update(Record record) {
		if (wHistory.size()<n) {
			wHistory.add(record);
			setScore(WmR_SCORE, Double.NaN);
		} else {
			setScore(WmR_SCORE, 100*(getHr()-record.getClose())
					/(getHr()-getLr()));
			wHistory.pollFirst();
			wHistory.add(record);
		}
	}
	
	/**
	 * 
	 * @return Lowest price in last n records
	 */
	private double getLr() {
		double rtn = Double.MAX_VALUE;
		for (Record r : wHistory) {
			if (r.getLow() < rtn) {
				rtn = r.getLow();
			}
		}
		return rtn;
	}
	
	/**
	 * 
	 * @return Highest price in last n records
	 */
	private double getHr() {
		double rtn = Double.MIN_VALUE;
		for (Record r : wHistory) {
			if (r.getHigh() > rtn) {
				rtn = r.getHigh();
			}
		}
		return rtn;
	}

	@Override
	public void merge(String code) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void change(double rate) {
		// TODO Auto-generated method stub
		
	}


}
